package com.xxgc.eow.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaIgnore;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import com.xxgc.eow.result.Result;

import java.time.LocalDateTime;
import java.util.List;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.xxgc.eow.service.IAddressService;
import com.xxgc.eow.entity.Address;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 企业地址表 前端控制器
 * </p>
 *
 * @author Mr.Abel
 * @since 2023-05-21
 */
@Tag(name = "企业地址表")
@RestController
@RequestMapping("/address")
public class AddressController {

    @Resource
    private IAddressService addressService;

    /**
     * 查询所有接口
     * @return
     */
    @ApiOperationSupport(author = "Mr.Abel")
    @Operation(summary = "查询所有接口", description = "查询企业地址表所有数据")
    @SaIgnore
    @GetMapping
    public Result<List<Address>> findAll() {
        return Result.ok(addressService.list());
    }

    /**
     * 根据id查询数据接口
     * @param companyId
     * @return
     */
    @ApiOperationSupport(author = "Mr.Abel")
    @Operation(summary = "根据企业id查询数据接口", description = "根据企业id查询")
    @Parameters({
            @Parameter(name = "companyId",description = "企业id",required = true,in=ParameterIn.QUERY)
    })
    @SaIgnore
    @GetMapping("/{companyId}")
    public Result<List<Address>> findOne(@PathVariable Integer companyId) {
        return Result.ok(addressService.getAllByCompanyId(companyId));
    }

    /**
     * 分页模糊查询接口
     * @param pageNum
     * @param pageSize
     * @param keyWord
     * @return
     */
    @ApiOperationSupport(author = "Mr.Abel")
    @Operation(summary = "分页模糊查询接口", description = "分页模糊查询")
    @Parameters({
            @Parameter(name = "pageNum",description = "页码",example="1",required = true,in=ParameterIn.QUERY),
            @Parameter(name = "pageSize",description = "每页多少条",example="10",required = true,in=ParameterIn.QUERY),
            @Parameter(name = "keyWord",description = "关键字",in=ParameterIn.QUERY)
    })
    @SaIgnore
    @GetMapping("/page")
    public Result<List<Address>> findPage(@RequestParam Integer pageNum,@RequestParam Integer pageSize,@RequestParam(required = false,defaultValue = "") String keyWord) {
        return Result.ok(addressService.getAllByKeyWord(pageNum,pageSize,keyWord));
    }

    /**
     * 新增接口
     * @param address
     * @return
     */
    @ApiOperationSupport(author = "Mr.Abel")
    @Operation(summary = "新增接口")
    @SaIgnore
    @PostMapping("add/")
    public Result save(@RequestBody Address address) {
        addressService.addAddress(address);
        return Result.ok();
    }

    /**
     * 更新接口
     * @param address
     * @param oldAddressName
     * @return
     */
    @ApiOperationSupport(author = "Mr.Abel")
    @Operation(summary = "更新接口", description = "根据旧的地址名")
    @SaIgnore
    @PostMapping("update")
    public Result updat(@RequestBody Address address ,String oldAddressName) {
        if(address.getCreateTime() == null){
            address.setCreateTime(LocalDateTime.now());
        }
        addressService.updatByOldName(address,oldAddressName);
        return Result.ok();
    }

    /**
     * 删除接口
     * @param companyId
     * @return
     */
    @ApiOperationSupport(author = "Mr.Abel")
    @Operation(summary = "通过id删除", description = "通过id删除")
    @Parameters({
            @Parameter(name = "companyId",description = "组件id",required = true,in=ParameterIn.QUERY)
    })
    @SaIgnore
    @DeleteMapping("deleteId/{companyId}")
    public Result delete(@PathVariable Integer companyId) {
        Address address =new Address();
        address.setCompanyId(companyId);
        address.setDestroyTime(LocalDateTime.now());
        address.setTip(1);
        addressService.delByCompanyId(address);
        return Result.ok();
    }

    /**
     * 删除接口
     * @param addressName
     * @return
     */
    @ApiOperationSupport(author = "Mr.Abel")
    @Operation(summary = "通过addressName删除", description = "通过addressName删除")
    @Parameters({
            @Parameter(name = "addressName",description = "addressName",required = true,in=ParameterIn.QUERY)
    })
    @SaIgnore
    @DeleteMapping("deleteByName/{addressName}")
    public Result deleteByName(@PathVariable String addressName) {
        Address address =new Address();
        address.setAddressName(addressName);
        address.setDestroyTime(LocalDateTime.now());
        address.setTip(1);
        addressService.delByAddressName(address);
        return Result.ok();
    }

    /**
     * 批量删除接口
     * @param ids
     * @return
     */
    @ApiOperationSupport(author = "Mr.Abel")
    @Operation(summary = "批量删除接口", description = "批量删除接口")
    @SaIgnore
    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        for (Integer id : ids) {
            Address address =new Address();
            address.setCompanyId(id);
            address.setDestroyTime(LocalDateTime.now());
            address.setTip(1);
            addressService.delByCompanyId(address);
        }
        return Result.ok();
    }
    /**
     * 查询总数
     * @param keyWord
     * @return
     */
    @ApiOperationSupport(author = "Mr.Abel")
    @Operation(summary = "模糊查询总数", description = "传入keyWord 模糊查询，不传查询所有")
    @Parameters({
            @Parameter(name = "keyWord",description = "关键字",in=ParameterIn.QUERY)
    })
    @SaIgnore
    @GetMapping("/count")
    public Result<List<Address>> findPage(@RequestParam(required = false,defaultValue = "") String keyWord) {
        if(addressService.getAllPage(keyWord).size() == 0){
            return Result.error(123,"未查询到相关信息","");
        }
        return Result.ok(addressService.getAllPage(keyWord));
    }
}